// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Invisible Block Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 38 : TYPE_INVISIBLEBLOCK

// Function declarations
reserve function InvisibleBlock_DebugDraw
reserve function InvisibleBlock_DebugSpawn

// Static Values
public value InvisibleBlock_value31 = 8;

// Tables

function InvisibleBlock_DebugDraw
	temp0 = object.xpos
	temp1 = object.ypos
	temp0 -= 0x80000
	temp1 -= 0x80000
	DrawSpriteXY(0, temp0, temp1)
	temp0 += 0x100000
	DrawSpriteXY(0, temp0, temp1)
	temp0 -= 0x100000
	temp1 += 0x100000
	DrawSpriteXY(0, temp0, temp1)
	temp0 += 0x100000
	DrawSpriteXY(0, temp0, temp1)
end function


function InvisibleBlock_DebugSpawn
	CreateTempObject(TypeName[Invisible Block], 17, object.xpos, object.ypos)
	object[tempObjectPos].drawOrder = 5
	object[tempObjectPos].value0 = 16
	object[tempObjectPos].value1 = 16
end function


event ObjectMain
	temp0 = object.value0
	FlipSign(temp0)
	temp1 = object.value1
	FlipSign(temp1)
	switch object.state
	case 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object[currentPlayer].state != PlayerObject_KnuxLedgePullUp
				BoxCollisionTest(C_BOX, object.entityPos, temp0, temp1, object.value0, object.value1, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				switch checkResult
				case 0
					temp0 += 2
					temp1 += 2
					temp2 = object.value0
					temp3 = object.value1
					temp2 -= 2
					temp3 -= 2
					BoxCollisionTest(C_TOUCH, object.entityPos, temp0, temp1, temp2, temp3, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						object[currentPlayer].gravity = GRAVITY_GROUND
					end if
					if currentPlayer == 0
						object.value2 = 0
					else
						object.value3 = 0
					end if
					break
				case 4
					if object[currentPlayer].gravity == GRAVITY_GROUND
						if currentPlayer == 0
							object.value2++
							if object.value2 > InvisibleBlock_value31
								CallFunction(PlayerObject_Kill)
							end if
						else
							object.value3++
							if object.value3 > InvisibleBlock_value31
								CallFunction(PlayerObject_Kill)
							end if
						end if
					end if
					break
				end switch
			end if
		next
		break
	case 1
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object[currentPlayer].state != PlayerObject_KnuxLedgePullUp
				BoxCollisionTest(C_TOUCH, object.entityPos, temp0, temp1, object.value0, object.value1, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					if object[currentPlayer].gravity == GRAVITY_GROUND
						object[currentPlayer].xpos = object[currentPlayer].collisionRight
						FlipSign(object[currentPlayer].xpos)
						object[currentPlayer].xpos -= object.value0
						object[currentPlayer].xpos <<= 16
						object[currentPlayer].xpos += object.xpos
						if object[currentPlayer].speed > 0
							object[currentPlayer].speed = 0
						end if
					end if
				end if
			end if
		next
		break
	case 2
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object[currentPlayer].state != PlayerObject_KnuxLedgePullUp
				BoxCollisionTest(C_TOUCH, object.entityPos, temp0, temp1, object.value0, object.value1, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					if object[currentPlayer].gravity == GRAVITY_GROUND
						object[currentPlayer].xpos = object[currentPlayer].collisionLeft
						FlipSign(object[currentPlayer].xpos)
						object[currentPlayer].xpos += object.value0
						object[currentPlayer].xpos <<= 16
						object[currentPlayer].xpos += object.xpos
						if object[currentPlayer].speed < 0
							object[currentPlayer].speed = 0
						end if
					end if
				end if
			end if
		next
		break
	end switch
end event


event ObjectDraw
	if object[0].type == TypeName[Debug Mode]
		temp0 = object.xpos
		temp1 = object.ypos
		temp2 = object.propertyValue
		temp4 = temp2
		temp4 &= 240
		temp4 <<= 15
		temp2 >>= 4
		temp2++
		temp6 = temp2
		temp3 = object.propertyValue
		temp3 &= 15
		temp5 = temp3
		temp5 <<= 19
		temp3++
		temp0 -= temp4
		temp4 = temp0
		temp1 -= temp5
		while temp3 > 0
			while temp2 > 0
				DrawSpriteXY(object.state, temp0, temp1)
				temp0 += 0x100000
				temp2--
			loop
			temp2 = temp6
			temp0 = temp4
			temp1 += 0x100000
			temp3--
		loop
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-8, -7, 16, 14, 1, 176)
	SpriteFrame(-8, -7, 16, 14, 17, 176)
	SpriteFrame(-8, -7, 16, 14, 1, 190)
	foreach (TypeName[Invisible Block], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 5
		object[arrayPos0].value0 = object[arrayPos0].propertyValue
		object[arrayPos0].value0 &= 240
		object[arrayPos0].value0 += 16
		object[arrayPos0].value0 >>= 1
		object[arrayPos0].value1 = object[arrayPos0].propertyValue
		object[arrayPos0].value1 &= 15
		object[arrayPos0].value1++
		object[arrayPos0].value1 <<= 3
	next
	CheckCurrentStageFolder("Zone09")
	if checkResult == 1
		InvisibleBlock_value31 = 1
	end if
	SetTableValue(TypeName[Invisible Block], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(InvisibleBlock_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(InvisibleBlock_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
